package com.xuanmiao.smsback.web.controller.operator;

import com.xuanmiao.smsback.domain.qo.UserInfoQO;
import com.panshi.hujin2.base.common.factory.MessageFactory;
import com.panshi.hujin2.base.common.util.DozerUtils;
import com.panshi.hujin2.base.domain.result.BasicResult;
import com.panshi.hujin2.base.domain.result.BasicResultCode;

import com.xuanmiao.smsback.web.controller.vo.UserInfoVO;
import com.xuanmiao.smsback.model.UserInfoBO;
import com.xuanmiao.smsback.service.platform.operator.UserInfoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Collections;
import java.util.List;

@Api("商户管理")
@RequestMapping(value = "/userInfo")
@RestController
@Slf4j
public class UserInfoController {
    private final Logger LOGGER = LoggerFactory.getLogger(getClass());
    @Autowired
    private UserInfoService userInfoService;

    @ApiOperation("商户账号管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "createTimeStart" ,value = "创建时间开始" ,dataType = "Date",paramType = "query"),
            @ApiImplicitParam(name = "createTimeEnd" ,value = "创建时间结束" ,dataType = "Date",paramType = "query"),
            @ApiImplicitParam(name = "status" ,value = "账户状态0--提交注册 1--注册被拒绝 2--激活 3--停用" ,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "createType" ,value = "创建方式0-注册 1-平台管理员添加 2-商户管理员添加" ,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "accountNo" ,value = "账户" ,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "companyName" ,value = "公司名称" ,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "businessNo" ,value = "商户号" ,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "contact" ,value = "联系人" ,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "businessArea" ,value = "营运区域 0--印尼" ,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "authOperator" ,value = "审核人" ,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "createOperator" ,value = "创建人" ,dataType = "String",paramType = "query"),

    })
    @GetMapping()
    BasicResult<List<UserInfoVO>> all(@ApiIgnore UserInfoQO userInfoQO){

        try {
            List<UserInfoBO> listBOs= userInfoService.getList(userInfoQO);
            List<UserInfoVO> listVOs = Collections.emptyList();

            if (listBOs.size() > 0) {
                listVOs = DozerUtils.convertList(listBOs, UserInfoVO.class);

                listVOs.stream().forEach(listVO -> {
                    if(listVO.getCreateType()==0){
                        listVO.setCreateOperator("用户注册");
                    }
                    if (listVO.getModifyTime() != null) {
                        listVO.setModifyTimeStr(DateFormatUtils.format(listVO.getModifyTime(), "yyyy/MM/dd HH:mm"));
                    }

                    listVO.setCreateTimeStr(DateFormatUtils.format(listVO.getCreateTime(), "yyyy/MM/dd HH:mm"));
                    if (listVO.getAuditTime() != null) {
                        listVO.setAuditTimeStr(DateFormatUtils.format(listVO.getAuditTime(), "yyyy/MM/dd HH:mm"));
                    }

                });
            }

            return BasicResult.ok(listVOs,userInfoQO.getPage());
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);

            return BasicResult.error(BasicResultCode.QUERY_FAIL.getCode(), MessageFactory.getMsg(BasicResultCode.QUERY_FAIL.getMessage()));
        }
    }
    @ApiOperation("账户名是否存在 返回ID")
    @GetMapping("/checkExist/{accountNo}")
    BasicResult<Integer> checkAccount(@PathVariable("accountNo") String accountNo) {
        try {
            Integer exist = userInfoService.existAccountNo(accountNo);
            return BasicResult.ok(exist);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return BasicResult.error(BasicResultCode.QUERY_FAIL.getCode(), BasicResultCode.QUERY_FAIL.getMessage());
        }
    }
    @ApiOperation("注册")
    @PostMapping("/register")
    BasicResult<Void> register(@Valid @RequestBody UserInfoVO userInfoVO) {
        try {

            userInfoService.register(DozerUtils.convert(userInfoVO, UserInfoBO.class));
            return BasicResult.ok();
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return BasicResult.error(BasicResultCode.SYS_EXCEPTION.getCode(), BasicResultCode.SYS_EXCEPTION.getMessage());
        }
    }
    @ApiOperation("主键查询商户数据")
    @GetMapping("{id:\\d+}")
    BasicResult<UserInfoVO> queryPrimaryKey(@PathVariable Integer id) {
        try {
            UserInfoBO res = userInfoService.getById(id);
            return BasicResult.ok(DozerUtils.convert(res, UserInfoVO.class));
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return BasicResult.error(BasicResultCode.QUERY_FAIL.getCode(), MessageFactory.getMsg(BasicResultCode.QUERY_FAIL.getMessage()));
        }
    }
    @ApiOperation("商户信息更新")
    @PutMapping("/update")
    BasicResult<Void> update(@Valid @RequestBody UserInfoVO userInfoVO) {
        try {
            UserInfoBO res = userInfoService.getById(userInfoVO.getId());
            if (res == null) {
                return BasicResult.error(BasicResultCode.DATA_NOT_EXIST.getCode(),  BasicResultCode.DATA_NOT_EXIST.getMessage());
            }
            userInfoService.update(DozerUtils.convert(userInfoVO, UserInfoBO.class));
            return BasicResult.ok();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return BasicResult.error(BasicResultCode.UPDATE_FAIL.getCode(), BasicResultCode.UPDATE_FAIL.getMessage());
        }
    }
}
